//
//  HsLCTradeLoginTimeChangeView.m
//  TestIos
//
//  Created by hs on 2020/10/22.
//  Copyright © 2020 Liyadong. All rights reserved.
//

#import "HsLCTradeLoginTimeChangeView.h"
#import "UIImage+colorImage.h"
#import "Masonry.h"

#define left_screen_margin     16

#define label_w     23

#define label_margin_between     (SCREEN_WIDTH - label_w * 5 - left_screen_margin) / 5

@interface HsLCTradeLoginTimeChangeView ()


@property (nonatomic, strong) UISlider *controlSlider;

@property (nonatomic, strong) UILabel *label1;
@property (nonatomic, strong) UILabel *label2;
@property (nonatomic, strong) UILabel *label3;
@property (nonatomic, strong) UILabel *label4;
@property (nonatomic, strong) UILabel *label5;

/** <#注释#> */
@property (nonatomic, strong) UIView *contenView;

@end


@implementation HsLCTradeLoginTimeChangeView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor greenColor];
        
        UIView *shadowView = [[UIView alloc] initWithFrame:CGRectZero];
        shadowView.backgroundColor = [UIColor greenColor];
        [self addSubview:shadowView];
        [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(left_screen_margin);
            make.bottom.right.mas_equalTo(-left_screen_margin);
        }];
        shadowView.layer.cornerRadius = 4;
        shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
        shadowView.layer.shadowRadius = 5;
        shadowView.layer.shadowOffset = CGSizeMake(0, 4);
        shadowView.layer.shadowOpacity = 0.1;
        
        
        UIView *contenView = [[UIView alloc] initWithFrame:CGRectZero];
        contenView.backgroundColor = [UIColor whiteColor];
        [self addSubview:contenView];
        [contenView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(left_screen_margin);
            make.bottom.right.mas_equalTo(-left_screen_margin);
        }];
        contenView.layer.cornerRadius = 4;
        contenView.layer.masksToBounds = YES;

        [contenView addSubview:self.controlSlider];

        self.contenView = contenView;

        [contenView addSubview:self.label1];
        [contenView addSubview:self.label2];
        [contenView addSubview:self.label3];
        [contenView addSubview:self.label4];
        [contenView addSubview:self.label5];


        [self.controlSlider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(left_screen_margin);
            make.bottom.mas_equalTo(self.mas_centerY).mas_offset(-4);
            make.right.mas_equalTo(-left_screen_margin);
            make.height.mas_equalTo(12);
        }];

        CGFloat item_margin = (SCREEN_WIDTH - left_screen_margin * 4) / 4;


        [self.label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_centerY).mas_offset(2);
            make.left.mas_equalTo(left_screen_margin);
            make.width.mas_equalTo(label_w);
        }];

        [self.label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.label1.mas_top);
            make.centerX.mas_equalTo(contenView.mas_left).mas_offset(item_margin + left_screen_margin);
            make.width.equalTo(self.label1.mas_width);
        }];

        [self.label3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.label1.mas_top);
            make.centerX.mas_equalTo(contenView.mas_left).mas_offset(item_margin * 2 + left_screen_margin);
            make.width.equalTo(self.label2.mas_width);
        }];

        [self.label4 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.label1.mas_top);
            make.centerX.mas_equalTo(contenView.mas_left).mas_offset(item_margin * 3 + left_screen_margin);
            make.width.equalTo(self.label3.mas_width);
        }];

        [self.label5 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.label1.mas_top);
            make.right.mas_equalTo(-left_screen_margin);
            make.width.equalTo(self.label4.mas_width);
        }];
        
        
    }
    return self;
}

 

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

#pragma mark - lazyload

- (UILabel *)label1 {
    if (!_label1) {
        _label1 = [self createMarkLabelWithStr:@"30" withIndex:0];
        _label1.textAlignment = NSTextAlignmentLeft;
        
    }
    return _label1;
}

- (UILabel *)label2 {
    if (!_label2) {
        _label2 = [self createMarkLabelWithStr:@"60" withIndex:1];
    }
    return _label2;
}


- (UILabel *)label3 {
    if (!_label3) {
        _label3 = [self createMarkLabelWithStr:@"120" withIndex:2];
    }
    return _label3;
}
- (UILabel *)label4 {
    if (!_label4) {
        _label4 = [self createMarkLabelWithStr:@"240" withIndex:3];
    }
    return _label4;
}

- (UILabel *)label5 {
    if (!_label5) {
        _label5 = [self createMarkLabelWithStr:@"360" withIndex:4];
        _label5.textAlignment = NSTextAlignmentRight;
    }
    return _label5;
}


- (UILabel *)createMarkLabelWithStr:(NSString *)tempStr withIndex:(NSInteger)index{
    UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    tempLabel.textColor = [UIColor colorWithHexString:@"#585C6C"];
    UIFont *font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    tempLabel.textAlignment = NSTextAlignmentCenter;
    tempLabel.font = font;
    tempLabel.tag = 1000 + index;
    tempLabel.text = tempStr;
    return tempLabel;
}


- (UISlider *)controlSlider{
    if(!_controlSlider){
        _controlSlider = [[UISlider alloc] initWithFrame:CGRectZero];
        _controlSlider.minimumTrackTintColor = [UIColor colorWithHexString:@"#EF4034"];
        _controlSlider.maximumTrackTintColor = [UIColor colorWithHexString:@"#EF4034"];

        _controlSlider.maximumValue = 4;
        _controlSlider.minimumValue = 0;
        _controlSlider.continuous = YES;
         
        UIImage *image = [UIImage imageWithColor:[UIColor colorWithHexString:@"#EF4034"] size:CGSizeMake(10, 10) cornerRadius:8];

        [_controlSlider setThumbImage:image forState:UIControlStateNormal];
        
        // 添加事件
//        [_controlSlider addTarget:self action:@selector(sliderValurChanged:forEvent:) forControlEvents:UIControlEventValueChanged];
        
        [_controlSlider addTarget:self
                    action:@selector(valueChanged:)
          forControlEvents:UIControlEventValueChanged];
       
    }
    return _controlSlider;
}

// 实现方法
- (void)sliderValurChanged:(UISlider*)slider forEvent:(UIEvent*)event {
    UITouch *touchEvent = [[event allTouches] anyObject];
    switch (touchEvent.phase) {
        case UITouchPhaseBegan:{
            NSLog(@"开始拖动");
         }
            break;
        case UITouchPhaseMoved: {
            NSLog(@"正在拖动");
 
        }
            break;
        case UITouchPhaseEnded:{
            NSLog(@"结束拖动");
 
        }
            break;
        default:
            break;
    }
}

- (void)valueChanged:(UISlider *)sender {
    NSUInteger index = (NSUInteger)(_controlSlider.value + 0.5);
    [_controlSlider setValue:index animated:YES];
    
    NSLog(@"slider index = %lu", (unsigned long)index);
    
    [self changeSelectWithIndex:index];
    
    if (self.timeCallBack) {
        self.timeCallBack(index);
    }
}
- (void)changeSelectWithIndex:(NSInteger)index {
    
    NSArray *subeViews = self.contenView.subviews;
    for (UIView *tempView in subeViews) {
        if ([tempView isKindOfClass:[UILabel class]] && tempView.tag - 1000 == index) {
            UILabel *tempLabel = (UILabel *)tempView;
            tempLabel.textColor = [UIColor colorWithHexString:@"#EF4034"];
        } else  if ([tempView isKindOfClass:[UILabel class]]) {
            UILabel *tempLabel = (UILabel *)tempView;
            tempLabel.textColor = [UIColor colorWithHexString:@"#585C6C"];
        }
    }
    
}

- (void)initTimeViewWithIndex:(NSInteger)index {
    [self.controlSlider setValue:index animated:NO];
    [self changeSelectWithIndex:index];
}



@end
